package aiku.numericalcontroloa.Service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import aiku.numericalcontroloa.Base.Utils.Result;
import aiku.numericalcontroloa.Mapper.wagesMapper;
import aiku.numericalcontroloa.Model.Dto.wagesDto;
import aiku.numericalcontroloa.Model.Entity.wages;
import aiku.numericalcontroloa.Service.wagesService;

@Service
public class wagesServiceImp implements wagesService {

    @Autowired
    private wagesMapper mapper;

    @SuppressWarnings("null")
    @Override
    public Result addWages(wagesDto data) {
        if (data == null && data.getUserId() == null && data.getWagesNumber() == null) {
            return Result.error("缺少参数");
        }
        LambdaQueryWrapper<wages> queryWrapper = new LambdaQueryWrapper<>();
        LocalDate starDate = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        LocalDate endDate = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
        queryWrapper.eq(wages::getUserId, data.getUserId()).ge(wages::getUpdateTime, starDate).le(wages::getUpdateTime,
                endDate);
        if (mapper.selectCount(queryWrapper) > 0) {
            return Result.success("已有此人");
        }
        wages Data = new wages();
        BeanUtils.copyProperties(data, Data);
        Data.setDeleted(0);
        if (mapper.insert(Data) == 1) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @Override
    public Result selectWages(wagesDto data) {
        if (data == null || data.getCurrentPage() == null || data.getPageSize() == null) {
            return Result.error("缺少分页参数");
        }
        LambdaQueryWrapper<wages> queryWrapper = new LambdaQueryWrapper<>();
        Page<wages> pages = new Page<>(data.getCurrentPage(), data.getPageSize());
        if (data.getTimeDate() != null) {
            LocalDate starDate = LocalDate.of(data.getTimeDate().getYear(), data.getTimeDate().getMonth(), 1);
            LocalDate endDate = starDate.plusMonths(1);
            queryWrapper.ge(wages::getUpdateTime, starDate).lt(wages::getUpdateTime, endDate);
        } else {
            return Result.error("缺少时间参数");
        }
        LocalDate inputLocalDate = LocalDate.of(data.getTimeDate().getYear(), data.getTimeDate().getMonth(), 1);
        LocalDate nowDate = LocalDate.now();
        List<wages> list;
        if (inputLocalDate.isAfter(nowDate)) {
            list = new ArrayList<>();
        } else {
            list = mapper.selectPage(pages, queryWrapper).getRecords();
        }
        return Result.success(list);
    }

    @Override
    public Result deleteWages(wagesDto data) {
        if (data == null || data.getId() == null) {
            return Result.error("缺少参数");
        }
        wages Data = new wages();
        BeanUtils.copyProperties(data, Data);
        if (!(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()).isAfter(mapper.selectById(data.getId())
                .getUpdateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()))
                && mapper.deleteById(Data) == 1) {
            return Result.success("删除成功");
        } else {
            return Result.success("删除失败");
        }
    }

    @Override
    public Result updateWages(wagesDto data) {
        if (data == null && data.getId() == null && data.getWagesNumber() == null) {
            return Result.error("缺少参数");
        }
        wages Data = new wages();
        BeanUtils.copyProperties(data, Data);
        if (!(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()).isAfter(mapper.selectById(data.getId())
                .getUpdateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()))
                && mapper.updateById(Data) == 1) {
            return Result.success("修改成功");
        } else {
            return Result.success("修改失败");
        }
    }
}